﻿#include "npport3d.h"
#include <QHash>
#include <qmetaobject.h>
#include "writelogmanager.h"
#include <QTextCodec>
#include "implementactivex.h"
#include <QVBoxLayout>
#include <QStackedWidget>
#include "configfileoperation.h"
#include "intarray.h"
#include "stringarray.h"
#include "doublearray.h"
#include "position.h"
#include "positionarray.h"
#include "objectentities.h"
#include "objectentitesarray.h"
#include "qtnpapi.h"

npPort3d::npPort3d(QWidget* parent)/*:QWidget(parent)*/
{
	loadLanguagePlugin();
	initialUI();
	_logOrNot = CoreBase::ConfigFileOperation::GetInstance().getBoolValue("openinterfacelog");
	writeLog("initialize","npPort3d",0);
}

npPort3d::~npPort3d()
{}

void npPort3d::release()
{
	delete _implementActivex;
	_implementActivex =NULL;
	QLayout* mainl = layout();
	delete mainl;
	_mainWidget->setParent(NULL);
	delete _mainWidget;
	_mainWidget = NULL;
	mainl = NULL;
	setLayout(mainl);
}

bool npPort3d::openMap(const QString& mapname)
{
	if(mapname!="" && _currentMapFullName != mapname)
	{
		_currentMapFullName = mapname;
		return _implementActivex->openMap(mapname);
	}
	return false;
}

bool npPort3d::closeMap()
{
	_currentMapFullName = "";
	return _implementActivex->closeMap();
}

void npPort3d::setScanParameterAt(int scanmode,double offsetxy,double offsetxz,double distance,double x,double y,double z)
{
	QString str = "--观察模式标志----" + QString::number(scanmode);
	str = str + "--距离----" + QString::number(distance);
	str = str + "--水平夹角----" + QString::number(offsetxy);
	str = str + "--竖直夹角----" + QString::number(offsetxz);
	str = str + "--观察中心点----" + QString::number(x)+","+QString::number(y)+","+QString::number(z);
	writeLog("setScanParameterAt",str,0);
	_implementActivex->setScanParameterAt(scanmode,offsetxy,offsetxz,distance,x,y,z);
}

void npPort3d::setScanParameterAt_2(int scanmode,double eyex,double eyey,double eyez,double centerx,double centery,
										double centerz,double upx,double upy,double upz)
{
	_implementActivex->setScanParameterAt_2(scanmode,eyex,eyey,eyez,centerx,centery,centerz,upx,upy,upz);
}

void npPort3d::moveCenterTo(double x,double y)
{
	_implementActivex->moveCenterTo(x,y);
}

void npPort3d::zoom(double ratio)
{
	_implementActivex->zoom(ratio);
}

void npPort3d::setLookAt(const QString& id,int offsetxy,int offsetxz,int distance)
{
	QString str = "--对准对象----" + id;
	str = str + "--距离----" + QString::number(distance);
	str = str + "--水平夹角----" + QString::number(offsetxy);
	str = str + "--竖直夹角----" + QString::number(offsetxz);
	writeLog("setLookAt",str,0);
	_implementActivex->setLookAt(id,offsetxy,offsetxz,distance);
}

void npPort3d::getLookAt(double& eyex,double& eyey,double& eyez,double& centerx,double& centery,
										double& centerz,double& upx,double& upy,double& upz,double& distance)
{
	_implementActivex->getLookAt(eyex,eyey,eyez,centerx,centery,centerz,upx,upy,upz,distance);
}

bool npPort3d::followObject(const QString& id,double centeroffsetx,double centeroffsety,double centeroffsetz,int distance,int anglexy, int anglexz)
{
	QString str = "--跟随对象----" + id;
	str = str + "--偏移x----" + QString::number(centeroffsetx);
	str = str + "--偏移y----" + QString::number(centeroffsety);
	str = str + "--偏移z----" + QString::number(centeroffsetz);
	str = str + "--距离----" + QString::number(distance);
	str = str + "--水平夹角----" + QString::number(anglexy);
	str = str + "--竖直夹角----" + QString::number(anglexz);
	writeLog("followObject",str,0);
	return _implementActivex->followObject(id,centeroffsetx,centeroffsety,centeroffsetz,distance,anglexy,anglexz);
}

void npPort3d::setScanMode(int mode)
{
	_implementActivex->setScanMode(mode);
	QString str = "--观察模式----" + QString::number(mode);
	writeLog("setScanMode",str,0);
}

void npPort3d::setScanSpeed(int speed)
{
	_implementActivex->setScanSpeed(speed);
}

void npPort3d::setToolTip(const QString& text)
{
	_implementActivex->setToolTip(text);
}

void npPort3d::setToolTipParameter(int textsize,int textr,int textg,int textb,int backgroundr,
	int backgroundg,int backgroundb,int backgrounda)
{
	_implementActivex->setToolTipParameter(textsize,textr,textg,textb,backgroundr,
			backgroundg,backgroundb,backgrounda);
}

void npPort3d::loadLanguagePlugin()
{
	_implementActivex = new ImplementActivex;
	if(QLocale::system().language() == QLocale::Chinese)
	{
		setlocale( LC_ALL, "chs" );
	}
	_implementActivex->setQtPluginObject();
	QTextCodec::setCodecForTr(QTextCodec::codecForName("GBK"));
}

void npPort3d::initialUI()
{
	QVBoxLayout* vLayout = new QVBoxLayout();
	vLayout->setContentsMargins(0,0,0,0);
	setLayout(vLayout);
	_mainWidget = new QStackedWidget();
	_mainWidget->addWidget(_implementActivex->getGLWidget());
	vLayout->addWidget(_mainWidget);
	_mainWidget->setCurrentWidget(_implementActivex->getGLWidget());
	connect(_implementActivex->getActivexView(),SIGNAL(signalsMouseLeftClickEvent(const int,const int,const double,const double,QString)),this,
		SLOT(slotsMouseLeftClickEvent(const int,const int,const double,const double,QString)));
	connect(_implementActivex->getActivexView(),SIGNAL(signalsMouseRightClickEvent(const int,const int,const double,const double,QString)),this,
		SLOT(slotsMouseRightClickEvent(const int,const int,const double,const double,QString)));
	connect(_implementActivex->getActivexView(),SIGNAL(signalsMouseDoubleClickEvent(const int,const int,const double,const double,QString)),this,
		SLOT(slotsMouseDoubleClickEvent(const int,const int,const double,const double,QString)));
	connect(_implementActivex->getActivexView(),SIGNAL(signalHoverEvent(const int,const int,const double,const double,QString)),this,
		SLOT(slotsHoverEvent(const int,const int,const double,const double,QString)));
	connect(_implementActivex->getGLWidget(),SIGNAL(signalBreakHoverEvent()),this,SLOT(slotsBreakHoverEvent()));
	connect(_implementActivex->getActivexView(),SIGNAL(signalsKeyPressEvent(const int,const int,const int,const double,const double)
		),this,SLOT(slotsKeyPressEvent(const int,const int,const int,const double,const double)));
	connect(_implementActivex->getActivexView(),SIGNAL(signalsToolButtonClick(QString)
		),this,SLOT(slotsToolButtonClick(QString)));
}

void npPort3d::writeLog(const QString& log1,const QString& log2,int priority)
{
	if(_logOrNot)
	{
		VrDataCore::WriteLogManager::GetInstance().writeLog(log1,log2);
	}
}

void npPort3d::resetTime(const QString& title)
{
	VrDataCore::WriteLogManager::GetInstance().resetStartTime(title);
}

void npPort3d::writeTimeLog(const QString& title)
{
	if(_logOrNot)
	{
		VrDataCore::WriteLogManager::GetInstance().writeTimeLog("timelog.txt",title);
	}
}

void npPort3d::createOrUpdateObjects(ObjectEntitiesArray* entitiesarray)
{
	_implementActivex->updateObjectsMotion(entitiesarray);
	for(int i=0;i<entitiesarray->size();++i)
	{
		QString str = entitiesarray->_objectEntitiesVector.at(i)->getObjectId();
		str = str + "--类型----" + QString::number(entitiesarray->_objectEntitiesVector.at(i)->getType());
		str = str + "--子类型----" + QString::number(entitiesarray->_objectEntitiesVector.at(i)->getSubType());
		str = str + "--平移----" + QString::number(entitiesarray->_objectEntitiesVector.at(i)->getPosition()->getPositionX());
		str = str + "," + QString::number(entitiesarray->_objectEntitiesVector.at(i)->getPosition()->getPositionY());
		str = str + "--旋转----" + QString::number(entitiesarray->_objectEntitiesVector.at(i)->getRotation());
		str = str + "--放缩----" + QString::number(entitiesarray->_objectEntitiesVector.at(i)->getScale());
		writeLog("createOrUpdateObjects",str,0);
	}
}

void npPort3d::setPropertyWindowParameter(int windowwidth,int windowheight,double namewidth,double valuewidth,
		int nametextsize,int valuetextsize,int nametextcolorr,int nametextcolorg,int nametextcolorb,int valuetextcolorr
		,int valuetextcolorg,int valuetextcolorb)
{
	_implementActivex->setPropertyWindowParameter(windowwidth,windowheight,namewidth,valuewidth,nametextsize,valuetextsize,nametextcolorr,
		nametextcolorg,nametextcolorb,valuetextcolorr,valuetextcolorg,valuetextcolorb);
}

bool npPort3d::deleteObject(const QString& id)
{
	writeLog("deleteObject",id,0);
	return _implementActivex->deleteObject(id);
}

bool npPort3d::createAutoMoveObjectWithTrack(ObjectEntities* entities, PositionArray* p,DoubleArray* t,bool loop)
{
	return _implementActivex->createAutoMoveObjectWithTrack(entities,p,t,loop);
}

bool npPort3d::createAutoUniformMoveObjectWithTrack(ObjectEntities* entities, PositionArray* p,double speed,bool loop)
{
	return _implementActivex->createAutoUniformMoveObjectWithTrack(entities,p,speed,loop);
}

//void npPort3d::updateProfessionProperty(ObjectEntities* entities)
//{
//	_implementActivex->updateProfessionProperty(entities);
//}

void npPort3d::showPropertyWidget(ObjectEntities* entities)
{
	QString str = entities->getObjectId();
	for(QHash<QString,QString>::Iterator iter = entities->_stringValue.begin();iter!=entities->_stringValue.end();++iter)
	{
		str = str + "--"+iter.key()+"---"+iter.value();
	}
	for(QHash<QString,int>::Iterator iter = entities->_intValue.begin();iter!=entities->_intValue.end();++iter)
	{
		str = str + "--"+iter.key()+"---"+QString::number(iter.value());
	}
	for(QHash<QString,double>::Iterator iter = entities->_floatValue.begin();iter!=entities->_floatValue.end();++iter)
	{
		str = str + "--"+iter.key()+"---"+QString::number(iter.value());
	}
	writeLog("showPropertyWidget",str,0);
	_implementActivex->showPropertyWidget(entities);
}

void npPort3d::hidePropertyWidget()
{
	_implementActivex->hidePropertyWidget();
}

void npPort3d::slotsHoverEvent(const int x,const int y,const double cx,const double cy,QString mess)
{
	emit hoverEvent(x,y,cx,cy,mess);
}

void npPort3d::slotsBreakHoverEvent()
{
	emit breakHoverEvent();
}

void npPort3d::slotsMouseLeftClickEvent(const int x,const int y,const double cx,const double cy,QString mess)
{
	emit mouseLeftClickEvent(x,y,cx,cy,mess);
	QString str = mess;
	str = str + "--屏幕坐标----" + QString::number(x) + "," +QString::number(y);
	str = str + "--三维坐标----" + QString::number(cx) + "," +QString::number(cy);
	writeLog("slotsMouseLeftClickEvent",str,0);
}

void npPort3d::slotsMouseRightClickEvent(const int x,const int y,const double cx,const double cy,QString mess)
{
	emit mouseRightClickEvent(x,y,cx,cy,mess);
	QString str = mess;
	str = str + "--屏幕坐标----" + QString::number(x) + "," +QString::number(y);
	str = str + "--三维坐标----" + QString::number(cx) + "," +QString::number(cy);
	writeLog("slotsMouseRightClickEvent",str,0);
}

void npPort3d::slotsMouseDoubleClickEvent(const int x,const int y,const double cx,const double cy,QString mess)
{
	emit mouseDoubleClickEvent(x,y,cx,cy,mess);
	QString str = mess;
	str = str + "--屏幕坐标----" + QString::number(x) + "," +QString::number(y);
	str = str + "--三维坐标----" + QString::number(cx) + "," +QString::number(cy);
	writeLog("slotsMouseDoubleClickEvent",str,0);
}

void npPort3d::slotsKeyPressEvent(const int k,const int x,const int y,const double cx,const double cy)
{
	emit keyPressEvent(k,-1,x,y,cx,cy);
	QString str = "--键值----" + QString::number(k);
	str = str + "--屏幕坐标----" + QString::number(x) + "," +QString::number(y);
	str = str + "--三维坐标----" + QString::number(cx) + "," +QString::number(cy);
	writeLog("slotsKeyPressEvent",str,0);
}

void npPort3d::slotsToolButtonClick(QString buttonname)
{
	emit toolButtonClickEvent(buttonname);
}

void npPort3d::markObject(const QString& ids,const QString& texts,int type,int r,int g,int b,int scal)
{
	_implementActivex->markObject(ids,texts,type,r,g,b,scal);
}

void npPort3d::unMarkObject(const QString& ids)
{
	_implementActivex->unMarkObject(ids);
}

void npPort3d::makeObjectColorBlink(const QString& ids,int totaltime,int intterval,int r,int g,int b,int a)
{
	_implementActivex->makeObjectColorBlink(ids,totaltime,intterval,r,g,b,a);
}

void npPort3d::stopObjectColorBlink(const QString& ids)
{
	_implementActivex->stopObjectColorBlink(ids);
}

void npPort3d::makeObjectBlink(const QString& ids,int totaltime,int intterval)
{
	_implementActivex->makeObjectBlink(ids,totaltime,intterval);
}

void npPort3d::stopObjectBlink(const QString& ids)
{
	_implementActivex->stopObjectBlink(ids);
}

void npPort3d::setCommondShapeParameter(int shapeType,int radious,int minh,int maxh,double minv,double maxv,int minr,int ming,int minb,
	int maxr,int maxg,int maxb,int a,bool autosize,bool canbeblock)
{
	_implementActivex->setCommondShapeParameter(shapeType,radious,minh,maxh,minv,maxv,minr,ming,minb,maxr,maxg,
		maxb,a,autosize,canbeblock);
}

void npPort3d::updateCommondShape(const QString& ids,const QString& values)
{
	_implementActivex->updateCommondShape(ids,values);
}

void npPort3d::clearCommondShape()
{
	_implementActivex->clearCommondShape();
}

void npPort3d::addTabsInteractionWindow(const QString& title,int r,int g,int b)
{
	_implementActivex->addTabsInteractionWindow(title,r,g,b);
	QString str = "--标题----" + title;
	str = str + "--颜色----" + QString::number(r) + "," +QString::number(g) + "," +QString::number(b);
	writeLog("addTabsInteractionWindow",str,0);
}

void npPort3d::removeTabsInteractionWindow(const QString& title)
{
	_implementActivex->removeTabsInteractionWindow(title);
	QString str = "--标题----" + title;
	writeLog("removeTabsInteractionWindow",str,0);
}

void npPort3d::addTextInteractionInformationWindow(const QString& text,const QString& title)
{
	_implementActivex->addTextInteractionInformationWindow(text,title);
	QString str = "--标题----" + title + "--文本----" + text;
	writeLog("addTextInteractionInformationWindow",str,0);
}

void npPort3d::setObjectsValidStatus(const QString& ids,const QString& status)
{
	_implementActivex->setObjectsValidStatus(ids,status);
}

bool npPort3d::showObject(const QString& id,bool state)
{
	QString str ="--ID----" + id;
	str = str + "--状态----" + (state?"显示":"隐藏");
	writeLog("showObject",str,0);
	return _implementActivex->showObject(id,state);
}

bool npPort3d::showObjects(StringArray* ids,bool state)
{
	QString str ="--ID----";
	for(int i=0;i<ids->size();++i)
	{
		str = str + ids->_valueVector.at(i)+",";
	}
	str = str + "--状态----" + (state?"显示":"隐藏");
	writeLog("showObjects",str,0);
	return _implementActivex->showObjects(ids->_valueVector,state);
}

bool npPort3d::showTarget(int type, int subtype,bool state)
{
	QString str ="--类型----" + QString::number(type);
	str = str + "--子类型----" + QString::number(subtype);
	str = str + "--状态----" + (state?"显示":"隐藏");
	writeLog("showTarget",str,0);
	return _implementActivex->showTarget(type,subtype,state);
}

void npPort3d::setDefaultResourceDir(const QString& dir)
{
	writeLog("setDefaultResourceDir",dir,0);
	_implementActivex->setDefaultResourceDir(dir);
}

void npPort3d::setDefaultLogFileDir(const QString& dir)
{
	VrDataCore::WriteLogManager::GetInstance().setDefaultLogPath(dir);
}

void npPort3d::showRemarkLayer(const QString& remarkname)
{
	_implementActivex->showRemarkLayer(remarkname);
}

void npPort3d::hideRemarkLayer(const QString& remarkname)
{
	_implementActivex->hideRemarkLayer(remarkname);
}

void npPort3d::showArea(const QString& areaname)
{
	_implementActivex->showArea(areaname);
}

void npPort3d::hideArea(const QString& areaname)
{
	_implementActivex->hideArea(areaname);
}

void npPort3d::addToolBarButton(int buttonType)
{
	_implementActivex->addToolBarButton(buttonType);
}

void npPort3d::setToolBarActive(int type,bool active)
{
	_implementActivex->setToolBarActive(type,active);
}

void npPort3d::addRemarkedAt(const QString& remarklayername,const QString& text,double x,double y,double z)
{
	_implementActivex->addRemarkedAt(remarklayername,text,x,y,z);
}

void npPort3d::removeRemark(const QString& remarklayername,const QString& text)
{
	_implementActivex->removeRemark(remarklayername,text);
}

void npPort3d::addAdvancedToolBarButton(int type,const QString& buttonname,const QString& buttonicon,const QString& buttoniconeffect)
{
	_implementActivex->addAdvancedToolBarButton(type,buttonname,buttonicon,buttoniconeffect);
}

QString npPort3d::getAllPlayCameraAnimationName()
{
	return _implementActivex->getAllPlayCameraAnimationName();
}

void npPort3d::playCameraAnimation(const QString& name)
{
	_implementActivex->playCameraAnimation(name);
}

void npPort3d::showGuideMapWindow()
{
	_implementActivex->showGuideMapWindow();
}

void npPort3d::hideGuideMapWindow()
{
	_implementActivex->hideGuideMapWindow();
}

QTNPFACTORY_BEGIN("npPort3d","port3d npapi")
	QTNPCLASS(npPort3d)
	QTNPCLASS(IntArray)
	QTNPCLASS(StringArray)
	QTNPCLASS(DoubleArray)
	QTNPCLASS(Position)
	QTNPCLASS(PositionArray)
	QTNPCLASS(ObjectEntities)
	QTNPCLASS(ObjectEntitiesArray)
QTNPFACTORY_END()